<?php

/**
 * This is the model class for table "mail".
 *
 * The followings are the available columns in table 'mail':
 * @property string $id
 * @property string $email
 * @property string $subject
 * @property string $body
 * @property string $status
 */
class Mail extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Mail the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'mail';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email, subject, body', 'required'),
			array('email, subject', 'length', 'max'=>255),
			array('status', 'length', 'max'=>7),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, email, subject, body, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'email' => 'Email',
			'subject' => 'Subject',
			'body' => 'Body',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('subject',$this->subject,true);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * send email from queue
	 */
	public function send() {
		if($this->status == 'waiting') {
			if(Mail::sendEmail($this->email, $this->body, $this->subject, 'akovalyov@gmail.com', 'Raz Raz')) {
				$this->status = 'sent';	
				$this->save(false);
			} else
				Log::saveLog("Can't send email");
		}
		return ;
	}
	
	/**
     * Send email
     * @param string $retriever - email 
     * @param string $text - mail body
     * @param string $subj - mail subject, not required
     * @param string $from - sender email
     * @param string $fromName - sender Name
     */
    public static function sendEmail($retriever, $text, $subj = '', $from = '', $fromName = '')
    {
    	$mailer = Yii::app()->mailer;
    	$config = Yii::app()->params['emailConfig'];
    	
    	$mailer->Host = $config['host'];
    	$mailer->Port = $config['port'];
    	$mailer->IsSMTP();
    	$mailer->CharSet = 'UTF-8';
    	$mailer->SMTPSecure = $config['secure'];
		$mailer->SMTPAuth = $config['auth'];
		$mailer->Username = $config['user'];
		$mailer->Password = $config['pass'];
    	$mailer->AddAddress($retriever);
		$mailer->Subject = $subj;
		$mailer->Body = $text;
		$mailer->From = $from;
		$mailer->FromName = $fromName;

    	return $mailer->Send();
    }
    
    /**
     * save email to db and send
     * @param $email
     * @param array mailData
     * @param send 
     */
    public static function saveEmail($email, $mailData, $send = true) { //  $send = false only for dev
    	$mail = new Mail;
		$mail->email = $email;
		$mail->subject = $mailData['subject'];
		$mail->body = $mailData['text'];
		$mail->save(false);
		//send to user email
		if($send)
			$mail->send();
    }
}